import 'package:chatgreat/helper/my_widget.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';

import 'package:get/get.dart';
import 'package:go_router/go_router.dart';

import '../../helper/AppUtil.dart';
import '../../helper/ability.dart';
import 'logic.dart';

/// 引导页
class GuidePage extends StatelessWidget {
  const GuidePage({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    final logic = Get.put(GuideLogic());
    return PageView(
      physics: const ClampingScrollPhysics(),
      controller: logic.pageController,
      onPageChanged: (index) {
        logic.state.currentPageIndex.value = index;
      },
      children: _guideWidgets(context),
    );
  }

  ///引导页子布局们
  List<Widget> _guideWidgets(BuildContext context) {
    return [
      _itemGuideWidget(
        index: 0,
        icon: 'images/yindao1.png',
        title: 'images/zi1.png',
        context: context,
      ),
      _itemGuideWidget(
        index: 1,
        icon: 'images/yindao2.png',
        title: 'images/zi2.png',
        context: context,
      ),
      _itemGuideWidget(
        index: 2,
        icon: 'images/yindao3.png',
        title: 'images/zi3.png',
        context: context,
      ),
    ];
  }

  ///单个子布局
  Widget _itemGuideWidget({
    required int index,
    required String icon,
    required String title,
    required BuildContext context
  }) {
    return Stack(
      children: [
        Positioned.fill(
          child: Image.asset(
            icon,
            fit: BoxFit.fill,
          ),
        ),
        Positioned(
          top: 64.h,
          left: 46.w,
          right: 46.w,
          child: Image.asset(
            title,
            fit: BoxFit.cover,
          ),
        ),
        Positioned(
            left: 16,
            right: 16,
            bottom: 46.h,
            height: 48,
            child: Container(
              decoration: const BoxDecoration(
                image: DecorationImage(
                  image: AssetImage('images/guide_button.png'),
                  fit: BoxFit.fill,
                ),
              ),
              child: TextButton(
                        child: MyTextWeight('下一步', 16, Colors.white, FontWeight.normal),
                        onPressed: () async {
                          if (index >= 2) {
                            var isLogin = await AppUtil().isLogin();
                            if (isLogin) {
                              context.go(Ability().homeRoute);
                            } else {
                              context.go('/login');
                            }
                            return;
                          }
                          final logic = Get.put(GuideLogic());
                          logic.startGuide();
                        },
                      ),
            ))
      ],
    );
  }

}
